<%= if @revision do %>
  <%= if Enum.empty?(@tree_path) do %>
    <div class="content"><%= @repo.description %></div>

    <div class="tabs is-toggle is-fullwidth">
      <ul>
        <li>
          <%= live_redirect to: Routes.codebase_path(@socket, :history, @repo.owner_login, @repo, @revision, []) do %>
            <span class="icon"><i class="fa fa-layer-group" aria-hidden="true"></i></span>
            <span>Commits&nbsp;&nbsp;<span class="tag"><%= @stats[:commits] || 0 %></span></span>
          <% end %>
        </li>
        <li>
          <%= live_redirect to: Routes.codebase_path(@socket, :branches, @repo.owner_login, @repo) do %>
            <span class="icon"><i class="fa fa-code-branch" aria-hidden="true"></i></span>
            <span>Branches&nbsp;&nbsp;<span class="tag"><%= @stats[:branches] || 0 %></span></span>
          <% end %>
        </li>
        <li>
          <%= live_redirect to: Routes.codebase_path(@socket, :tags, @repo.owner_login, @repo) do %>
            <span class="icon"><i class="fa fa-tags" aria-hidden="true"></i></span>
            <span>Tags&nbsp;&nbsp;<span class="tag"><%= @stats[:tags] || 0 %></span></span>
          <% end %>
        </li>
        <li>
          <a>
            <span class="icon"><i class="fa fa-user-friends" aria-hidden="true"></i></span>
            <span>Contributors&nbsp;&nbsp;<span class="tag"><%= @stats[:contributors] || 0 %></span></span>
          </a>
        </li>
      </ul>
    </div>
  <% end %>

  <nav class="level">
    <div class="level-left">
      <div class="level-item">
        <div class="branch-select">
          <%= live_component(@socket, GitGud.Web.BranchSelectLive,
            id: "branch_select",
            repo: @repo,
            agent: @agent,
            revision: @revision,
            commit: @commit,
            tree_path: @tree_path,
            action: :tree
          ) %>
        </div>
      </div>
      <div class="level-item">
        <nav id="tree-breadcrumb" class="breadcrumb" aria-label="breadcrumbs" phx-hook="TreeBreadcrumb">
          <ul>
            <li>
              <%= live_patch @repo.name, to: Routes.codebase_path(@socket, :tree, @repo.owner_login, @repo, @revision, []) %>
            </li>
            <%= unless Enum.empty?(@tree_path) do %>
              <%= for {path, index} <- Enum.with_index(Enum.drop(@tree_path, -1), 1) do %>
                <li><%= live_patch path, to: Routes.codebase_path(@socket, :tree, @repo.owner_login, @repo, @revision, Enum.take(@tree_path, index)) %></li>
              <% end %>
              <li class="is-active"><a><%= List.last(@tree_path) %></a></li>
            <% end %>
            <li></li>
          </ul>
        </nav>
      </div>
    </div>
    <div class="level-right">
      <%= if Enum.empty?(@tree_path) do %>
        <div id="clone-repo" class="level-item clone-repo" phx-hook="CloneRepo">
          <div class="field has-addons">
            <div class="control"><button class="button http-clone-button is-active">HTTP</button></div>
            <%= if @current_user do %>
              <div class="control"><button class="button ssh-clone-button">SSH</button></div>
            <% end %>
            <div class="control http-clone">
              <input class="input" type="text" value={Routes.codebase_url(@socket, :show, @repo.owner_login, @repo) <> ".git"} readonly />
            </div>
            <%= if @current_user do %>
              <div class="control ssh-clone is-hidden">
                <%= case Keyword.get(Application.get_env(:gitgud, GitGud.SSHServer), :port, 22) do %>
                  <% 22 -> %>
                    <input class="input" type="text" value={"#{@current_user.login}@#{GitGud.Web.Endpoint.struct_url().host}:#{@repo.owner_login}/#{@repo.name}.git"} readonly />
                  <% ssh_port ->%>
                    <input class="input" type="text" value={"ssh://#{@current_user.login}@#{GitGud.Web.Endpoint.struct_url().host}:#{ssh_port}/#{@repo.owner_login}/#{@repo.name}.git"} readonly />
                <% end %>
              </div>
            <% end %>
            <div class="control">
              <button class="button clipboard-button tooltip" data-tooltip="Copy to clipboard">
                <span class="icon">
                  <i class="fa fa-clipboard"></i>
                </span>
              </button>
            </div>
          </div>
        </div>
      <% end %>
      <%= if revision_type(@revision) == :branch && authorized?(@current_user, @repo, :push) do %>
        <div class="level-item">
          <%= live_redirect "New", to: Routes.codebase_path(@socket, :new, @repo.owner_login, @repo.name, @revision, @tree_path), class: "button" %>
        </div>
      <% end %>
      <%= unless Enum.empty?(@tree_path) do %>
        <div class="level-item">
          <%= live_redirect "History", to: Routes.codebase_path(@socket, :history, @repo.owner_login, @repo.name, @revision, @tree_path), class: "button" %>
        </div>
      <% end %>
    </div>
  </nav>

  <table class="table tree-table is-hoverable is-fullwidth">
    <thead>
      <tr>
        <td colspan="2">
          <%= if @tree_commit_info do %>
            <%= with %{author: _author, committer: committer} <- Map.take(@tree_commit_info, [:author, :committer]) do %>
              <%= if Map.has_key?(committer, :id) do %>
                <a href={Routes.user_path(@socket, :show, committer)} class="tag is-white user"><%= committer %></a>
              <% else %>
                <a href={"mailto:#{committer.email}"} class="tag tooltip is-white has-text-black" data-tooltip={committer.email}><%= committer.name %></a>
              <% end %>
              <%= live_redirect commit_message_title(@tree_commit_info.message), to: Routes.codebase_path(@socket, :commit, @repo.owner_login, @repo, oid_fmt(@tree_commit_info.oid)), class: "has-text-dark" %>
            <% end %>
          <% else %>
            <span class="tag placeholder is-white"></span>
          <% end %>
        </td>
        <td class="has-text-right">
          <%= if @tree_commit_info do %>
            <%= datetime_format(@tree_commit_info.timestamp, "{relative}") %>
          <% else %>
            <span class="tag placeholder is-white"></span>
          <% end %>
        </td>
      </tr>
    </thead>
    <tbody>
      <%= for {tree_entry, commit_info} <- @tree_entries do %>
        <tr>
          <td>
            <%= case tree_entry.type do %>
              <% :tree -> %>
                <%= live_patch to: Routes.codebase_path(@socket, :tree, @repo.owner_login, @repo, @revision, @tree_path ++ [tree_entry.name]) do %>
                  <span class="icon"><i class="fa fa-folder"></i></span>
                  <span><%= tree_entry.name %></span>
                <% end %>
              <% :blob -> %>
                <%= live_redirect to: Routes.codebase_path(@socket, :blob, @repo.owner_login, @repo, @revision, @tree_path ++ [tree_entry.name]) do %>
                  <span class="icon"><i class="fa fa-file"></i></span>
                  <span><%= tree_entry.name %></span>
                <% end %>
            <% end %>
          </td>
          <td class="is-fullwidth">
            <%= if commit_info do %>
              <%= live_redirect commit_message_title(commit_info.message), to: Routes.codebase_path(@socket, :commit, @repo.owner_login, @repo, oid_fmt(commit_info.oid)), class: "has-text-dark" %>
            <% end %>
          </td>
          <td class="has-text-right has-text-dark">
            <%= if commit_info do %>
              <%= datetime_format(commit_info.timestamp, "{relative}") %>
            <% end %>
          </td>
        </tr>
      <% end %>
    </tbody>
  </table>

  <%= with {readme, filename} <- @tree_readme do %>
    <div class="card">
      <header class="card-header">
        <div class="card-header-title"><%= filename %></div>
          <p class="card-header-icon buttons">
            <%= live_redirect to: Routes.codebase_path(@socket, :blob, @repo.owner_login, @repo, @revision, @tree_path ++ [filename]), class: "button is-small is-link is-inverted" do %>
              <span class="icon"><i class="fa fa-code"></i></span>
            <% end %>
          </p>
      </header>
      <div class="card-content">
        <div class="content"><%= readme %></div>
      </div>
    </div>
  <% end %>
<% else %>
  <div class="content">
    <p><%= @repo.description %></p>
  </div>

  <h1 class="title">Quick setup</h1>

  <div class="content">
    <p>create a new repository on the command line</p>
    <pre>
    echo "# foobar" >> README.md
    git init
    git add README.md
    git commit -m "first commit"
    git remote add origin <%= Routes.codebase_url(@socket, :show, @repo.owner_login, @repo) %>.git
    git push -u origin master
    </pre>

    <p>or push an existing repository from the command line</p>
    <pre>
    git remote add origin <%= Routes.codebase_url(@socket, :show, @repo.owner_login, @repo) %>.git
    git push -u origin master
    </pre>
  </div>
<% end %>
